package com.eurasia.assignment.controller;

import java.util.List;
import java.util.stream.Collectors;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.eurasia.assignment.controller.dto.CreateProjectAssignmentRequest;
import com.eurasia.assignment.controller.dto.SubmissionResponse;
import com.eurasia.assignment.controller.request.SubmissionReviewRequest;
import com.eurasia.assignment.domain.Submission;
import com.eurasia.assignment.service.port.in.CreateProjectAssignmentUseCase;
import com.eurasia.assignment.service.port.in.CreateSubmissionReviewUseCase;
import com.eurasia.assignment.service.port.in.CreateSubmissionUseCase;
import com.eurasia.assignment.service.port.in.GetProjectSubmissionsUseCase;

import lombok.RequiredArgsConstructor;

@RestController
@RequestMapping("/projects/{projectId}/assignment")
@RequiredArgsConstructor
public class ProjectAssignmentController {
    private final CreateProjectAssignmentUseCase createProjectAssignmentUseCase;
    private final CreateSubmissionUseCase createSubmissionUseCase;
    private final GetProjectSubmissionsUseCase getProjectSubmissionsUseCase;
    private final CreateSubmissionReviewUseCase createSubmissionReviewUseCase;

    @PostMapping()
    @ResponseStatus(org.springframework.http.HttpStatus.CREATED)
    public void createProjectAssignment(@PathVariable Long projectId, @RequestBody CreateProjectAssignmentRequest request) {
        CreateProjectAssignmentUseCase.CreateProjectAssignmentCommand command = new CreateProjectAssignmentUseCase.CreateProjectAssignmentCommand(projectId, request.getTeamLeadId(), request.getTeamMemberIds());
        createProjectAssignmentUseCase.createProjectAssignment(command);
    }

    @PostMapping("/submissions")
    @ResponseStatus(org.springframework.http.HttpStatus.CREATED)
    public void createSubmission(@PathVariable Long projectId,
                                 @RequestParam String description,
                                 @RequestParam String type,
                                 @RequestParam String submitterId,
                                 @RequestParam("file") MultipartFile file) {
        CreateSubmissionUseCase.CreateSubmissionCommand command =
                new CreateSubmissionUseCase.CreateSubmissionCommand(
                        projectId, description, type, submitterId, file);
        createSubmissionUseCase.createSubmission(command);
    }

    @PostMapping("/submissions/{submissionId}/review")
    @ResponseStatus(org.springframework.http.HttpStatus.CREATED)
    public void createSubmissionReview(
            @PathVariable Long submissionId,
            @RequestBody SubmissionReviewRequest request) {
        createSubmissionReviewUseCase.createReview(submissionId, request.action(), request.reviewResult(), request.reviewerId());
    }

    @GetMapping("/submissions")
    public ResponseEntity<List<SubmissionResponse>> getAllSubmissions(@PathVariable Long projectId) {
        List<Submission> submissions = getProjectSubmissionsUseCase.getAllSubmissionsByProjectId(projectId);
        List<SubmissionResponse> submissionResponses = submissions.stream()
                .map(SubmissionResponse::fromDomain)
                .collect(Collectors.toList());
        return ResponseEntity.ok(submissionResponses);
    }
}
